<?php
	require_once 'Configuration.php';
	require_once LIB_PATH . '/nusoap/nusoap.php';
	
	$server = new soap_server();
	// Create the WSDL
	$server->configureWSDL('HirdWeb Basic Webservice Example', 'urn:hellowsdl');
	$server->wsdl->addComplexType('showPhrases','complexType','struct','all','',
	    array(
	        'status' => array('name' => 'status', 'type' => 'xsd:string'),
	        'phrase' => array('name' => 'message', 'type' => 'xsd:string')
	    )
	);
	
	$server->wsdl->addComplexType(
'ContactArray',
'complexType',
'array','',
'SOAP-ENC:Array',
array(),
array(
array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:showPhrases[]')
), 'tns:showPhrases');
	
	// Set up all the items to register the function
	$in = array(
		'id' => 'xsd:string',
		'name' => 'xsd:string',
	);
	$out = array('return' => 'tns:ContactArray');
	$namespace = 'uri:hirdwebservice';
	$soapaction = 'uri:hirdwebservice/showPhrases';
	$doc = 'Shows a phrase from Shakespeare and replaces the name with the supplied name. ID is a number from 0 - 4';
	$server->register('showPhrases', $in, $out, $namespace, $soapaction, 'rpc', 'encoded', $doc);
	
	function showPhrases($id, $name){
	// Set up the phrases
	$phrases = array(
		'O NnnnnnnnnN, NnnnnnnnnN! wherefore art thou NnnnnnnnnN?',
		'But NnnnnnnnnN, If you prick us, do we not bleed? if you tickle us, do we not laugh? if you poison us, do we not die? and if you wrong us, shall we not revenge?',
		'Will all great NnnnnnnnnN\'s ocean wash this blood clean from my hand? No, this my hand will rather the multitudinous seas incarnadine, making the green one red',
		'Love looks not with the eyes, but with the mind, NnnnnnnnnN, and therefore is winged Cupid painted blind',
		'Men at some time are masters of their fates: The fault, dear NnnnnnnnnN, is not in our stars, but in ourselves, that we are underlings',
	);

	// place all validation code here
	if ( ($id < 0) || ($id > 4)  ){
		// return an error
		$fin_data[] = array('status'=>"Error!", 'phrase'=>"The ID was outside of the allowed range");
		return $fin_data;
	}

	// replace all the needles with the name
	$phrase = preg_replace("/NnnnnnnnnN/", $name, $phrases[$id]);

	$fin_data[] = array('status'=>"Success!", 'phrase'=>$phrase);
	$fin_data[] = array('status'=>"Success2!", 'phrase'=>$phrase."2");

	return $fin_data;
}
	$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
	$server->service($HTTP_RAW_POST_DATA);
	exit; 
	
	
	//da una lista di bean ad array
	/*function searchAddressBook($query) {
    $contacts = Contact::search($query);
    //return $contacts;
    $results = array();
    foreach ($contacts as $contact) {
            $tempArray = array( 'id' => $contact->get(),
                                'first_name' => $contact->first_name,
                                'last_name' => $contact->last_name,
                                'email' => $contact->email,
                                'phone_number' => $contact->phone_number
                                            );
            array_push($results, $tempArray);
    }
 
    return $results;
}*/
